package com.suse.warmstill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.suse.warmstill.common.enums.RedisEnum;
import com.suse.warmstill.common.enums.ResponseEnum;
import com.suse.warmstill.common.exception.BusinessException;
import com.suse.warmstill.common.utils.JwtUtil;
import com.suse.warmstill.common.utils.RedisCache;
import com.suse.warmstill.entity.TbToken;
import com.suse.warmstill.mapper.TbTokenMapper;
import com.suse.warmstill.service.TokenService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author 黄靖
 * @author 2022-10-19 20:11
 * description:
 **/
@Service
@Slf4j
public class TokenServiceImpl implements TokenService {
    @Autowired
    private TbTokenMapper tbTokenMapper;

    @Autowired
    private RedisCache redisCache;


    @Override
    public Long checkToken(String token) {

        int userIdToken = redisCache.getCacheObject(RedisEnum.LOGIN_TOKEN + token);
        Long userIdJwt = JwtUtil.getUserId(token);


        if (ObjectUtils.isEmpty(userIdToken)) {
            log.info("redis中无数据");
            return null;
        }
        if (userIdJwt == userIdToken) {
            return userIdJwt;
        } else {
            return null;
        }


    }

    @Override
    public String getaccesstoken(Long id) {
        final String accessToken = JwtUtil.createJWT(60 * 12, id);


        return accessToken;
    }

    @Override
    public String getRefleshToken(Long id) {
        //存储一周
        final String refleshToken = JwtUtil.createJWT(60 * 24 * 7, id);
        return refleshToken;
    }


    @Override
    public String relreshToken(String token) {
        /*
         * 1、数据库查找 refreshtoken
         * 2、分析token
         * 3、生成 accesstoken 返回给前端
         * */
        final LambdaQueryWrapper<TbToken> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbToken::getAccessToken, token);

        final TbToken token1 = tbTokenMapper.selectOne(wrapper);
        if (ObjectUtils.isEmpty(token1)) {
            log.info("数据库中没有 该token");
            throw new BusinessException(ResponseEnum.NO_OBJECT.getCode(), ResponseEnum.NO_OBJECT.getMsg());
        }


        final String refreshToken = token1.getRefreshToken();
        final Long userId = JwtUtil.getUserId(refreshToken);
        final String accessToken = JwtUtil.createJWT(30, userId);

        /*
         * 更新数据库
         * */
        CompletableFuture.runAsync(() -> {
            token1.setAccessToken(accessToken);
            tbTokenMapper.updateById(token1);
        });

        /*
         * 更新 redis
         * */
        CompletableFuture.runAsync(() -> {
            redisCache.setCacheObject(accessToken, null, 30, TimeUnit.MINUTES);
        });
        return accessToken;
    }


    @Override
    public void insetToken(TbToken token) {
        tbTokenMapper.insert(token);
    }
}
